define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'project/projectapply/index' + location.search,
                    add_url: 'project/projectapply/add',
                    //edit_url: 'project/projectapply/edit',
                    del_url: 'project/projectapply/del',
                    multi_url: 'project/projectapply/multi',
                    import_url: 'project/projectapply/import',
                    table: 'project_apply',
                },
                queryParams: function (params) { //自定义搜索条件
                    var filter = params.filter ? JSON.parse(params.filter) : {}; //判断当前是否还有其他高级搜索栏的条件
                    var op = params.op ? JSON.parse(params.op) : {};  //并将搜索过滤器 转为对象方便我们追加条件
                    if(Config.staff_id){
                        filter.staff_id= Config.staff_id;     //将透传的参数 Config.group_id，追加到搜索条件中
                        op.staff_id= "=";  //group_id的操作方法的为 找到相等的
                        params.filter = JSON.stringify(filter); //将搜索过滤器和操作方法 都转为JSON字符串
                        params.op = JSON.stringify(op);
                    }

                    //如果希望忽略搜索栏搜索条件,可使用
                    //params.filter = JSON.stringify({url: 'login'});
                    //params.op = JSON.stringify({url: 'like'});
                    return params;

                },
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'staff_id', title: __('Staff_id')},
                        {field: 'project_id', title: __('Project_id')},
                        {field: 'category_id', title: __('Category_id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'unit', title: __('Unit'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'remark', title: __('Remark'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'price', title: __('Price')},
                        {field: 'explain', title: __('服务说明')},
                        {field: 'work_day', title: __('工作日'), operate: false},
                        {field: 'service_start_time', title: __('服务开始时间')},
                        {field: 'service_end_time', title: __('服务结束时间')},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {0: __('审核中'), 1: __('服务中'), 2: __('已暂停'), 3: __('已删除'), 4: __('审核拒绝')}},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons:[
                                {
                                    name: 'audit',
                                    text: __('审核通过'),
                                    confirm:'确认审核通过该服务吗?',
                                    icon: '',
                                    refresh:true,
                                    classname: 'btn btn-info btn-xs btn-detail  btn-ajax',
                                    url: 'project/projectapply/pass',
                                    visible:function(row){
                                        if(row.status == 0){
                                            return true;
                                        }
                                    }
                                },
                                {
                                    name: 'refuse',
                                    text: __('审核拒绝'),
                                    confirm:'确认审核拒绝该服务吗?',
                                    icon: '',
                                    refresh:true,
                                    classname: 'btn btn-danger btn-xs btn-detail  btn-ajax',
                                    url: 'project/projectapply/refuse',
                                    visible:function(row){
                                        if(row.status == 0){
                                            return true;
                                        }
                                    }
                                },
                                {
                                    name: 'recommend',
                                    text: __('推荐'),
                                    confirm:'确认推荐该服务吗?',
                                    icon: '',
                                    refresh:true,
                                    classname: 'btn btn-info btn-xs btn-detail  btn-ajax',
                                    url: 'project/projectapply/recommend',
                                    visible:function(row){
                                        if(row.is_recommend == 0){
                                            return true;
                                        }
                                    }
                                },
                                {
                                    name: 'cancel_recommend',
                                    text: __('取消推荐'),
                                    confirm:'确认取消推荐该服务吗?',
                                    icon: '',
                                    refresh:true,
                                    classname: 'btn btn-info btn-xs btn-detail  btn-ajax',
                                    url: 'project/projectapply/cancelrecommend',
                                    visible:function(row){
                                        if(row.is_recommend == 1){
                                            return true;
                                        }
                                    }
                                },
                                {
                                    name: 'settop',
                                    text: __('置顶'),
                                    confirm:'确认置顶吗?',
                                    icon: '',
                                    refresh:true,
                                    classname: 'btn btn-info btn-xs btn-detail  btn-ajax',
                                    url: 'project/projectapply/settop',
                                    visible:function(row){
                                        if(row.is_top == 0){
                                            return true;
                                        }
                                    }
                                },
                                {
                                    name: 'cancel_top',
                                    text: __('取消置顶'),
                                    confirm:'确认取消置顶吗?',
                                    icon: '',
                                    refresh:true,
                                    classname: 'btn btn-info btn-xs btn-detail  btn-ajax',
                                    url: 'project/projectapply/canceltop',
                                    visible:function(row){
                                        if(row.is_top == 1){
                                            return true;
                                        }
                                    }
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
